#include "EventLoop.h"
#include "Acceptor.h"
#include "TcpConnection.h"
#include <iostream>
#include <unistd.h>

using std::cout;
using std::endl;

//连接建立做的事件
void onConnection(const TcpConnectionPtr &con)
{
    cout << con->toString() << " has connected111!!!" << endl; 
}

//消息到达做的事件
void onMessage(const TcpConnectionPtr &con)
{
    string msg = con->receive();
    cout << ">>recv msg from client : " << msg << endl;

    con->send(msg);
}

//连接断开做的事件
void onClose(const TcpConnectionPtr &con)
{
    cout << con->toString() << " has closed111!!!" << endl; 
}

void test()
{
    //让服务器处于监听状态
    Acceptor acceptor("127.0.0.1", 8888);
    acceptor.ready();

    //创建EventLoop对象，启动epoll
    EventLoop loop(acceptor);
    loop.setNewConnectionCallback(std::move(onConnection));
    loop.setMessageCallback(std::move(onMessage));
    loop.setCloseCallback(std::move(onClose));
    loop.loop();//循环启动的标志
}

int main(int argc, char **argv)
{
    test();
    return 0;
}

